from watchdog.events import *

try:
    from main import import_database
except:
    import import_database


class FileEventHandler(FileSystemEventHandler):
    def __init__(self):
        FileSystemEventHandler.__init__(self)

    # def on_moved(self, event):
    #     if event.is_directory:
    #         print("directory moved from {0} to {1}".format(event.src_path, event.dest_path))
    #     else:
    #         print("file moved from {0} to {1}".format(event.src_path, event.dest_path))
    #
    # def on_created(self, event):
    #     if event.is_directory:
    #         print("directory created:{0}".format(event.src_path))
    #     else:
    #         print("file created:{0}".format(event.src_path))
    #
    # def on_deleted(self, event):
    #     if event.is_directory:
    #         print("directory deleted:{0}".format(event.src_path))
    #     else:
    #         print("file deleted:{0}".format(event.src_path))

    def on_modified(self, event):
        if event.is_directory is False:
        #     print("directory modified:{0}".format(event.src_path))
        # else:
        #     print("file modified:{0}".format(event.src_path))
            try:
                f = open(event.src_path, "r")
                f.close()
                print("加载完成...开始导入数据库")
                import_database.import_database(event.src_path)
            except Exception as e:
                print("文件正在加载请稍候...")

